package com.xlh.controller.course;

import com.google.common.collect.Lists;
import com.xlh.common.Result;
import com.xlh.dto.course.StepDTO;
import com.xlh.param.course.StepParam;
import com.xlh.service.course.StepService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * 步骤
 * <p>
 * Created by lx on 2020/6/12.
 */
@Api(tags = "StepController", description = "课程步骤")
@RestController
@RequestMapping("/course/chapter/{chapterId}/step")
public class StepController {

    @Autowired
    private StepService stepService;

    @ApiOperation("获取某章节的步骤列表")
    @GetMapping("/list")
    @PreAuthorize("permitAll()")
    public Result<List<StepDTO>> listStep(
            @PathVariable("chapterId") @ApiParam(value = "章节id", required = true) Long chapterId) {
        return Result.success(stepService.listStep(chapterId));
    }

    @ApiOperation("添加步骤")
    @PostMapping()
    @PreAuthorize("@permissionValidator.isAdminOrTeacher()")
    public Result createStep(
            @PathVariable("chapterId") @ApiParam(value = "章节id", required = true) Long chapterId,
            @RequestBody @Valid StepParam param) {
        stepService.createStep(chapterId, param);
        return Result.success();
    }

    @ApiOperation("修改某步骤")
    @PutMapping("/{id}")
    @PreAuthorize("@permissionValidator.isAdminOrTeacher()")
    public Result updateStep(@PathVariable("chapterId") @ApiParam(value = "章节id", required = true) Long chapterId,
                             @PathVariable("id") @ApiParam(value = "步骤id", required = true) Long id,
                             @RequestBody @Valid StepParam param) {
        stepService.updateStepInfo(chapterId, id, param);
        return Result.success();
    }

    @ApiOperation("删除某步骤")
    @DeleteMapping("/{id}")
    @PreAuthorize("@permissionValidator.isAdminOrTeacher()")
    public Result deleteStep(@PathVariable("chapterId") @ApiParam(value = "章节id", required = true) Long chapterId,
                             @PathVariable("id") @ApiParam(value = "步骤id", required = true) Long id) {
        stepService.deleteStep(chapterId, Lists.newArrayList(id), true);
        return Result.success();
    }

}
